package com.tianqingwl.gsystem.resources.controller;

import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.ResultVo;
import com.tianqingwl.gsystem.resources.pojo.SysPermission;
import com.tianqingwl.gsystem.resources.service.SysPermissionService;
import com.tianqingwl.gsystem.resources.vo.PermissionVo;
import com.tianqingwl.gsystem.sysuser.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/manage/permission")
@Controller
@PreAuthorize("@gs.check('sys_manage_permission')")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

    @ResponseBody
    @GetMapping("/list")
    public Object list(String roleName,Long roleId,Boolean roleIn) {
        List<PermissionVo> permissionVoList = sysPermissionService.listPermissionVo(roleId,roleIn);
        return CommonResultVo.success(permissionVoList);
    }

    @RequestMapping({"/permission-edit.html"})
    public String permissionEdit(Model model, Long perId, Long parentId) {
        if (perId != null) {
            //修改查询
            SysPermission permission = sysPermissionService.getSysPermission(perId);
            if (permission == null) {
                return "redirect:404.html";
            }
            model.addAttribute("sysPermission", permission);
        }
        //无论是新增还是修改都可获取父级权限资源
        if (parentId != null) {
            SysPermission parentSysPermission = sysPermissionService.getSysPermission(parentId);
            model.addAttribute("parentSysPermission", parentSysPermission);
        }
        return "manage/permission/permission-edit";
    }

    @PostMapping({"/permissionSave"})
    @ResponseBody
    public ResultVo permissionSave(SysPermission sysPermission) {
        try {
            SysUser sysUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            sysPermission.setCreator(sysUser.getUsername());
            SysPermission saveResult = sysPermissionService.save(sysPermission);
            return CommonResultVo.success(saveResult);
        } catch (Exception e) {
            return CommonResultVo.fail(e.getMessage());
        }
    }

    @GetMapping({"/delete"})
    @ResponseBody
    public ResultVo delete(Long perId) {
        try {
            sysPermissionService.deleteById(perId);
            return CommonResultVo.success();
        } catch (Exception e) {
            return CommonResultVo.fail(e.getMessage());
        }
    }

    @GetMapping("/remove")
    @ResponseBody
    public Object remove(Long roleId,Long[] permissionIds){
        if (roleId!=null&&permissionIds!=null&&permissionIds.length>0){
            sysPermissionService.removeRolePermission(roleId,permissionIds);
            return CommonResultVo.success();
        }
        return CommonResultVo.fail("参数不足");
    }
}
